var priceforclient = (function (e){
    var initials = false, cID, pID;

    e.initAddNewContactPrice = function(clientID){
        cID = clientID;
        pID = 0;
        e.init();
    };
    e.editAddNewContactPrice = function(contactPriceID, clientID){
        pID = contactPriceID;
        cID = clientID;
        e.init();
        dataBase.getPricesByID(pID, function(data){
            $("#selGroupsOfServices option[value='" + data.GroupsOfServicesID + "']").attr('selected','selected');
            $("#selGroupsOfServices").selectmenu('refresh', true);
            RefreshService(data.GroupsOfServicesID, function(){
            $("#selServices option[value='" + data.ServicesID + "']").attr('selected','selected');
            $("#selServices").selectmenu('refresh', true);
            });
            $("#selVolumeUnits option[value='" + data.VolumeUnitsID + "']").attr('selected','selected');
            $("#selVolumeUnits").selectmenu('refresh', true);
            $("#txtPriceFC").val(data.Price);
        });

    };
    function ResetGui(){
        //$("#selFromLenguage").val("");
        //$("#selToLanguage").val("");
        //$("#selPriceType").val("");
        $("#txtPriceFC").val("");
        //$("#selPriceType").val("");
    };
    function FillData(){
        return {
            PriceID:pID,
            ClientID:cID,
            GroupsOfServicesID: parseInt($("#selGroupsOfServices option:selected").val()),
            ServicesID: parseInt($("#selServices option:selected").val()),
            VolumeUnitsID: parseInt($("#selVolumeUnits option:selected").val()),
            Price: parseFloat($("#txtPriceFC").val())
        } ;

    };
    function RefreshService(gsID, action){
        dataBase.getServicesList(gsID, function(data){
            var i= 0, num = data.length, str=[];
            for(i;i<num;i+=1){
                str.push("<option value='" + data[i].ServicesID + "'>" + data[i].Services + "</option> ");
            }
            $("#selServices").html(str.join('')).selectmenu('refresh', true);
            if(action)
                action();
        });
    }
    e.init = function(){
        if(!initials){
            $("#btnPriceFCEdit").click(function (){
                if(pID === 0){
                    var newData = FillData();
                    dataBase.insertPrices(newData,function (){
                        pricesforclient.loadPricedForClient(cID);
                        ResetGui();
                        $.mobile.changePage("#pnPriceForClient");
                    });
                }else{
                    var newData = FillData();
                    dataBase.editPrices(newData, function (clientID){
                        pricesforclient.loadPricedForClient(cID);
                        ResetGui();
                        $.mobile.changePage("#pnPriceForClient");
                    });
                }
            });
            $("#btnPriceFCBack").click( function (){
                pricesforclient.loadPricedForClient(cID);
                $.mobile.changePage("#pnPriceForClient");
            });
            dataBase.getGroupsOfServicesList(function(data){
                var i= 0, num = data.length, str=[];
                for(i;i<num;i+=1){
                    str.push("<option value='" + data[i].GroupsOfServicesID + "'>" + data[i].GroupName + "</option> ");
                }
                $("#selGroupsOfServices").html(str.join('')).selectmenu('refresh', true);
                RefreshService(data[0].GroupsOfServicesID);
            });
            $("#selGroupsOfServices").live("change", function() {
                RefreshService($("#selGroupsOfServices option:selected").val());
            });
            dataBase.getVolumeUnitsList(function(data){
                var i= 0, num = data.length, str=[];
                for(i;i<num;i+=1){
                    str.push("<option value='" + data[i].VolumeUnitsID + "'>" + data[i].Name + "</option> ");
                }
                $("#selVolumeUnits").html(str.join('')).selectmenu('refresh', true);
            });
        }
        initials = true;
    };
    return e;
}(priceforclient || {}));
